package com.stylefeng.guns.modular.vpn.utils;

import com.stylefeng.guns.core.base.tips.ErrorTip;
import com.stylefeng.guns.core.common.exception.BizExceptionEnum;
import com.stylefeng.guns.core.exception.GunsException;
import com.stylefeng.guns.modular.vpn_server_info.entity.AllowIp;
import com.stylefeng.guns.modular.vpn_server_info.entity.VpnServerDTO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Optional;

/**
 * @Description VPN服务器节点工具类
 * @Author Mr.GodRui
 * @Date 2020-04-08 15:34
 **/
@Slf4j
public class ServerIPUtil {
    /**
     * 获取所有的allowIP
     * @param vpnServerDTO
     * @return
     */
    public static Optional<List<AllowIp>> getAllowIPs(VpnServerDTO vpnServerDTO){
        String[] p1 = vpnServerDTO.getBeginSegment().split("\\.");
        String[] p2 = vpnServerDTO.getEndSegment().split("\\.");

        StringBuffer sb = new StringBuffer();

        sb.append(p1[0]).append(".").append(p1[1]).append(".");

        int b1 = Integer.parseInt(p1[2]);
        int a1 = Integer.parseInt(p1[3]);

        int b2 = Integer.parseInt(p2[2]);
        int a2 = Integer.parseInt(p2[3]);

        List<AllowIp> allowIpList = new ArrayList<>();

        if(b1 == b2){
            //只循环添加末位网段
            for (int i = a1; i <= a2; i++){
                allowIpList.add(getOneAllowIP(sb.toString() + b1+"."+i,vpnServerDTO.getServerId()));
            }
        }else{
            for (int i = b1; i <= b2; i++) {
                if(i == b1){
                    //第一个二级网段b1，末位网段从a1开始到255
                    for (int j = a1; j <= 255; j++) {
                        allowIpList.add(getOneAllowIP(sb.toString() + i+"."+j,vpnServerDTO.getServerId()));
                    }
                }else{
                    //大于第一个二级网段b1，末位网段从0到255
                    for (int j = 0; j <= 255; j++) {
                        if(i == b2 && j > a2){
                            //最后一个二级网段时，末位网段从0到a2
                            break;
                        }
                        allowIpList.add(getOneAllowIP(sb.toString() + i+"."+j,vpnServerDTO.getServerId()));
                    }
                }
            }
        }
        return Optional.of(allowIpList);
    }

    /**
     * 得到一个allowip 对象
     * @param ipAddr
     * @param serverId
     * @return
     */
    private static AllowIp getOneAllowIP(String ipAddr,Integer serverId){
        AllowIp allowIp = new AllowIp();
        allowIp.setIpAddress(ipAddr);
        allowIp.setServerId(serverId);
        allowIp.setStatus(1);
        allowIp.setCreateTime(new Date());
        allowIp.setUpdateTime(new Date());
        return allowIp;
    }

    public static boolean isTrueOfSegment(String beginSegment,String endSegment){
        if(StringUtils.isEmpty(beginSegment)
                || StringUtils.isEmpty(endSegment)){
            return false;
        }

        String[] p1 = beginSegment.split("\\.");
        String[] p2 = endSegment.split("\\.");

        if(p1.length != 4
                || p2.length != 4
                || !(p1[0]+p1[1]).equals(p2[0]+p2[1])
                || Integer.parseInt(p1[2]) > Integer.parseInt(p2[2])){
            return false;
        }
        return true;
    }
}
